$(function() {
  // 下拉选择
  $(function() {
    $('.pm-select').each(function() {
      var
        select = $(this),
        input = select.find('input'),
        ul = select.find('ul'),
        arrow = select.find('.select-btn')

      ul.delegate('li', 'mousedown', function() {
        input.val($(this).text())
        input.attr('data-value', $(this).attr('value'))
      })

      input.on('focus', function() {
        if (select.is('[disabled]')) return
        if (window.innerHeight - select.offset().top < 280) {
          ul.css('bottom', 29)
          ul.css('top', 'unset')
        } else {
          ul.css('top', 29)
          ul.css('bottom', 'unset')
        }
        ul.css('display', 'block')
      })

      select.on('mousedown', function(ev) {
        if (select.is('[disabled]')) {
          ev.preventDefault()
        }
      })

      input.on('blur', function() {
        ul.css('display', 'none')
      })

      arrow.click(function() {
        if (select.is('[disabled]')) return
        input.trigger('focus')
      })

      input.on('input', function() {
        var
          txt = $(this).val(),
          reg = new RegExp(txt)

        if (txt.trim() === '') {
          ul.find('li').each(function() {
            $(this).css('display', 'block')
          })
        } else {
          ul.find('li').each(function() {
            if (reg.test($(this).text())) {
              $(this).css('display', 'block')
            } else {
              $(this).css('display', 'none')
            }
          })
        }
      })
    })
  })

  // 功能按钮交互
  $('.pm-operating .pm-button').on('click', function() {
    $('.pm-operating .pm-button').removeClass('act')
    $(this).addClass('act')
  })

  // 展开高级搜索
  $('.adv-search').click(function() {
    var wrap = $('.form-s-h')

    if (wrap.css('display') === 'none') {
      var oh = wrap.height()

      wrap.height(0).css({'display': 'block', 'overflow': 'hidden'})

      wrap.animate({height: oh}, 150, function() {
        wrap.css('overflow', 'visible')
      })
    } else {
      wrap.css('overflow', 'hidden')
      wrap.animate({height: 0}, 150, function() {

        wrap.css({'display': 'none', 'height': 'auto'})
      })
    }
  })

  // 排序
  $('.table-sort').click(function() {
    alert('排序')
  })

  // 重置
  $('.pm-form .reset').click(function() {
    alert('重置')
  })

  // 表格
  var
    easyui_table = $('#easyui_table'),
    easyui_table_data = [{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    },{
      name: '珠江地产巡逻展名称名称',
      code: 'Z20051454544158084',
      status: '合同打印中',
      nature: '主合同',
      kind: '采购类合同',
      amount: '2,221.00',
      count: '30,001.00',
      profit: '0',
      hgName: '广东珠江工程总承包有限公司',
      qdType: '',
      fbType: '直接发包',
      czqk: '',
      time: '2019-02-22',
      jbr: '郑婷婷',
      qiqu: '一期',
      qiqu2: '一期'
    }]

  easyui_table.datagrid({
    rownumbers: true,
    autoRowHeight: false,
    striped: true,
    width: '100%',
    singleSelect: true,
    pagination: true,
    data: easyui_table_data,
    frozenColumns: [[
      {field:'name', title:'合同名称'},
      {field:'code', title:'合同编号', width:186}
    ]],
    columns: [[
      {field:'status', title:'合同状态', width:86},
      {field:'nature', title:'合同性质', width:82},
      {field:'kind', title:'合同大类', width:82},
      {field:'amount', title:'合同金额', width:82},
      {field:'count', title:'合同总金额', width:140},
      {field:'profit', title:'计入成本金额', width:104},
      {field:'hgName', title:'合同/规划名称'},
      {field:'qdType', title:'签订方类型', width:96},
      {field:'fbType', title:'发包类型', width:66},
      {field:'czqk', title:'产值情况', width:74},
      {field:'time', title:'合同签订时间'},
      {field:'jbr', title:'经办人', width:120},
      {field:'qiqu', title:'期区', width:120},
      {field:'qiqu2', title:'期区2', width:120}
    ]]
  })
  
  // 开启首行筛选
  easyui_table.datagrid('enableFilter', {
    remoteFilter: true
  })

  // $(window).on('resize', function() {
  //   easyui_table.datagrid('reload')
  // })

  // 分页显示中文
  var tablePager = easyui_table.datagrid('getPager')

  $(tablePager).pagination({
      beforePageText: '第', //页数文本框前显示的汉字  
      afterPageText: '页    共 {pages} 页'
      // displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
  })

  // 添加过滤图标
  var
    filterIcon = $('<i class="filter-icon"></i>'),
    datagridFilterInput = $('.datagrid .datagrid-filter-row .datagrid-filter-c input')

  $('.datagrid .datagrid-filter-row .datagrid-filter-c').append(filterIcon)

  datagridFilterInput.unbind('keydown').on('keydown', function(ev) {
    var field = $(this).parents('td').attr('field')
    if (ev.keyCode == 13) {
      alert('筛选 ' + field + ' 字段')
    }
  })
})